package com.liholin.springboot.sample.exception

import com.liholin.springboot.sample.configdata.ResponseData
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.bind.annotation.RestControllerAdvice

//作为springmvc的异常处理
@RestControllerAdvice
class ProjectExceptionAdvice {

    //所有异常都会进入
    /**
     * 500 - Internal Server Error
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(ServiceException::class)
    fun handleServiceException(e: ServiceException): ResponseData {
        e.printStackTrace()
        return e.getErrorResult()
    }

    /**
     * 500 - Internal Server Error
     */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(Exception::class)
    fun handleException(e: Exception): ResponseData {
        e.printStackTrace()
        return ResponseData.error(ErrorStatus.ERROR_500.apply { errorMessageZh = e.toString() })
    }
}